package com.example.hkydemo;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class LoginActivity extends AppCompatActivity {

    private TextView tv1;
    private EditText edName;
    private EditText edPwd;
    private Button btn1;
    private Button btn2;

    private Integer stats = 1;
    private EditText edStuid;
    private LinearLayout main;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        DbContect dbContect = new DbContect(LoginActivity.this);
        SQLiteDatabase db = dbContect.getWritableDatabase();
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stats = 0;
                tv1.setText("注册");
                btn1.setText("注册");
                main.setVisibility(View.VISIBLE);
                btn2.setVisibility(View.GONE);
            }
        });
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (stats == 1) {
                    String strStuid = "";
                    String strPwd = "";
                    String edStrPwd = String.valueOf(edPwd.getText());
                    String edStrId = String.valueOf(edStuid.getText());
                    Cursor cursor = db.rawQuery("select * from student where stuid = ? ", new String[]{edStrId});
                    if (cursor.getCount() != 0) {
                        if (cursor.moveToFirst()) {
                            do {//逐项读出数据
                                String id = cursor.getString(0);
                                strStuid = cursor.getString(2);
                                strPwd = cursor.getString(3);
                                if (strStuid.equals(edStrId) && strPwd.equals(edStrPwd)) {
                                    Intent intent = new Intent(LoginActivity.this, IndexActivity.class);
                                    intent.putExtra("id", id);
                                    startActivity(intent);
                                    finish();
                                } else {
                                    showNormalDialog();
                                }
                            } while (cursor.moveToNext());
                        }
                    } else {
                        showNormalDialog();
                    }
                } else {
                    String edStrPwd = String.valueOf(edPwd.getText());
                    String edStrName = String.valueOf(edName.getText());
                    String edStrId = String.valueOf(edStuid.getText());
                    Cursor cursor = db.rawQuery("select * from student where stuid = ?", new String[]{edStrId});
                    if (cursor.getCount() == 0) {
                        ContentValues values = new ContentValues();
                        values.put("name", edStrName);
                        values.put("stuid", edStrId);
                        values.put("password", edStrPwd);
                        long id = db.insert("student", null, values);
                        stats = 1;
                        tv1.setText("登录");
                        btn1.setText("登录");
                        main.setVisibility(View.GONE);
                    } else {
                        final AlertDialog.Builder normalDialog =
                                new AlertDialog.Builder(LoginActivity.this);
                        normalDialog.setTitle("这个学号已重复");
                        normalDialog.setPositiveButton("确定",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        //...To-do
                                        Log.i("TAG", "onClick: 点击确定注册");
                                        normalDialog.create();
                                    }
                                });
                        normalDialog.setNegativeButton("返回登录",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        //...To-do
                                        Log.i("TAG", "onClick: 点击重试");
                                        normalDialog.create();
                                        stats = 1;
                                        tv1.setText("登录");
                                        btn1.setText("登录");
                                        main.setVisibility(View.GONE);
                                    }
                                });
                        // 显示
                        normalDialog.show();
                    }

                }


            }
        });
    }

    private void initView() {
        tv1 = (TextView) findViewById(R.id.tv1);
        edName = (EditText) findViewById(R.id.ed_name);
        edPwd = (EditText) findViewById(R.id.ed_pwd);
        btn1 = (Button) findViewById(R.id.btn_1);
        btn2 = (Button) findViewById(R.id.btn_2);
        edStuid = (EditText) findViewById(R.id.ed_stuid);
        main = (LinearLayout) findViewById(R.id.main);
    }

    private void showNormalDialog() {
        /* @setIcon 设置对话框图标
         * @setTitle 设置对话框标题
         * @setMessage 设置对话框消息提示
         * setXXX方法返回Dialog对象，因此可以链式设置属性
         */
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(LoginActivity.this);
        normalDialog.setTitle("是否注册新用户?");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                        Log.i("TAG", "onClick: 点击确定注册");
                        normalDialog.create();
                        stats = 0;
                        tv1.setText("注册");
                        btn1.setText("注册");
                        main.setVisibility(View.VISIBLE);
                    }
                });
        normalDialog.setNegativeButton("重试",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                        Log.i("TAG", "onClick: 点击重试");
                        normalDialog.create();
                    }
                });
        // 显示
        normalDialog.show();
    }
}